package com.jabo.douban.demo.test;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jabo.douban.demo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @anthor JZheng3
 * @Time: 2020/3/5
 */
public class TextViewActivity extends AppCompatActivity {

    private List<MyMessage> mMyMessages = new ArrayList<>();
    private MyMessage mCurrentMymessage;
    int i = 0;
    private Thread mThread;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

        final TextView viewById = findViewById(R.id.tvTest);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("zjb", "onClick: ");
                MyMessage myMessage = new MyMessage();
                myMessage.when = System.currentTimeMillis();
                myMessage.name = String.valueOf(++i);
                showMessage(myMessage);
            }
        });

//        viewById.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                final MyImageGetter imageGetter = new MyImageGetter(TextViewActivity.this, viewById);
//                String html = "哈哈 我是郑家\n波\n<img src=\"https://img4.caishuib.com/upload/images/20200226/1582683364877068712.jpg\" >";
//                final String html1 = "<img src=\"https://img4.caishuib.com/upload/images/face/jz19.gif\" alt=\"[合作]\">请问还有其他需要帮助的吗？";
//                String html2 = "哈哈 我是郑家波哈哈 我是郑家波哈哈 我是郑家波哈哈 我是郑家波哈哈 我是郑家波<img src=\"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1035415831,1465727770&fm=26&gp=0.jpg\" border=\"0\">";
//                viewById.setText(Html.fromHtml(html, imageGetter, null));
//            }
//        }, 5000);


        mThread = new Thread(new Runnable() {
            @Override
            public void run() {

                for (; ; ) {
                    synchronized (this) {
                        if (mCurrentMymessage != null) {
                            long timeMillis = System.currentTimeMillis();
                            if (mCurrentMymessage.when <= timeMillis) {
                                if (mMyMessages.size() > 1) {
                                    mCurrentMymessage.close();
                                    mMyMessages.remove(mCurrentMymessage);
                                    mCurrentMymessage = mMyMessages.get(0);
                                    mCurrentMymessage.show();
                                    mCurrentMymessage.when = System.currentTimeMillis() + 1000;
                                }
                            }
                        } else {
                            if (mMyMessages.size() > 0)
                                mCurrentMymessage = mMyMessages.get(0);
                        }

                        if (mMyMessages == null) {
                            break;
                        }
                    }
                }

            }
        });
        mThread.start();

    }

    private void showMessage(MyMessage message) {
        if (mMyMessages == null) {
            mMyMessages = new ArrayList<>();
        }

        if (mCurrentMymessage == null) {
            message.when += 1000;
            message.show();
//            Log.e("zjb", "showMessage:show: ");
        }
        mMyMessages.add(message);
    }

    private void hideMessage(MyMessage message) {
        if (mMyMessages != null) {
            mMyMessages.remove(message);
        }
    }


    private void quit() {
        mMyMessages = null;
    }


    class MyMessage {
        public long when;
        public String name;

        public void close() {
            Log.e("zjb", "name:" + name + "close: " + when);
        }

        public void show() {
            Log.e("zjb", "name:" + name + "show: " + when);
        }
    }
}
